package com.gsnd.gsnd22estore.web.controller;

import com.alipay.api.domain.Shop;
import com.gsnd.gsnd22estore.domain.EsShopcar;
import com.gsnd.gsnd22estore.dto.ShopCarInfoDTO;
import com.gsnd.gsnd22estore.result.Result;
import com.gsnd.gsnd22estore.service.ShopCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/shopCar")
public class ShopCarController {

    @Autowired
    ShopCarService shopCarService;

    @GetMapping
    public Result<List<ShopCarInfoDTO>> getShopCarInfoByUser(HttpServletRequest request){
        Integer userId = Integer.parseInt((String) request.getAttribute("userId"));
        return Result.success(shopCarService.getShopCarByUser(userId));
    }

    @DeleteMapping
    public Result<String> removeShopCatItems(@RequestBody List<Integer> ids){
        shopCarService.removeBookInfoInShopCar(ids);
        return Result.success("删除成功");
    }

    @PutMapping("/{id}")
    public Result<String> addBookToShopCar(@PathVariable Integer id,
                                   HttpServletRequest request){
        Integer userId = Integer.parseInt((String) request.getAttribute("userId"));
        EsShopcar shopCar = new EsShopcar();
        shopCar.setUserId(userId);
        shopCar.setBookId(id);
        shopCar.setNum(1);
        shopCarService.addBookToShopCar(shopCar);
        return Result.success("添加成功");
    }

    @PutMapping("/{id}/{num}")
    public Result<String> editBookNum(@PathVariable Integer id,
                              @PathVariable Integer num,
                              HttpServletRequest request){
        EsShopcar shopCar = new EsShopcar();
        shopCar.setId(id);
        shopCar.setNum(num);
        shopCarService.modifyShopCar(shopCar);
        return Result.success("数量更新成功");
    }

}
